package entity

import (
	"save-kindle/source/ddd/definition"
	"save-kindle/source/ddd/dp"
)

// Catalog 目录: 根据给定的目录网址，解析 章节
type Catalog struct {
	dp.BookCatalog

	CatalogParser definition.CatalogParser

	chapterList []*Chapter
}

func newCatalog(catalog dp.BookCatalog, parser definition.Parser) *Catalog {
	// 调用网站对应的解析器
	// 解析
	// 获取目录
	c := &Catalog{
		BookCatalog:   catalog,
		CatalogParser: parser,
	}
	c.getChapterList(parser)
	return c
}

func (catalog *Catalog) getChapterList(parser definition.Parser) []*Chapter {
	if catalog.chapterList != nil {
		return catalog.chapterList
	}

	bookChapterList := catalog.CatalogParser.ParserChapterList(catalog.BookCatalog)
	chapterList := make([]*Chapter, len(bookChapterList))
	for i := range bookChapterList {
		// 标题
		chapterList[i] = NewChapter(bookChapterList[i], parser)
	}

	catalog.chapterList = chapterList

	return chapterList
}
